/*
 ------------------------------------------------------------------------------
 Copyright (C) 2009-2011 Eternal Games.

 This file is part of the Eternal Tech source code.

 The Eternal Tech source code is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or (at your
 option) any later version.

 The Eternal Tech source code is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 more details.

 You should have received a copy of the GNU General Public License along with
 the Eternal Tech source code; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 ------------------------------------------------------------------------------
*/


//
// fileSystem.h - ...
//


#if !defined(__FILESYSTEM_H__) || !INCLUDE_GUARDS
#define __FILESYSTEM_H__


// Max length of a file system path
#define	MAX_PATH_LENGTH					256

// Max number of pack files loaded
#define MAX_PACK_FILES					128

// Default game directory
#define BASE_DIRECTORY					"baseq2"

// These directories will be ignored by file and directory scanning functions
#define CODE_DIRECTORY					"code"
#define DOCS_DIRECTORY					"docs"
#define SVN_DIRECTORY					".svn"

typedef int							fileHandle_t;

enum fsMode_t {
	FS_READ,
	FS_WRITE,
	FS_APPEND
};

// Copies the given file
bool				FS_CopyFile2 (const char *srcName, const char *dstName);

// Renames the given file
bool				FS_RenameFile2 (const char *oldName, const char *newName);

// Returns true if the given file exists in the current game directory.
// This is to determine if writing a file will cause any overwrites.
bool				FS_FileExists2 (const char *name);

// Reads data from a file, properly handling partial reads.
// Returns the number of bytes read.
int					FS_Read2 (fileHandle_t f, void *buffer, int size);

// Writes data to a file, properly handling partial writes.
// Returns the number of bytes written.
int					FS_Write2 (fileHandle_t f, const void *buffer, int size);

// Writes formatted text to a file.
// Returns the number of characters written.
int					FS_Printf (fileHandle_t f, const char *fmt, ...);

// Opens the given file for reading, writing, or appending. Will create any
// needed subdirectories.
// Returns file size, or -1 if the file wasn't found or an error occurred.
int					FS_OpenFile (const char *name, fsMode_t mode, fileHandle_t *f);

// Closes the given file
void				FS_CloseFile (fileHandle_t f);

// Reads a complete file.
// Returns file size, or -1 if the file wasn't found or an error occurred.
// A NULL buffer will just return the file size without loading as a quick
// check for existance (-1 == not present).
// Appends a trailing 0, so string operations are always safe.
int					FS_ReadFile (const char *name, void **buffer);

// Writes a complete file. Will create any needed subdirectories.
// Returns true if the file was written.
bool				FS_WriteFile2 (const char *name, const void *buffer, int size);

// Frees the memory allocated by FS_ReadFile
void				FS_FreeFile2 (const void *buffer);

// Returns a list of files and subdirectories that match the given extension
// (which must include a leading '.' and must not contain wildcards).
// If extension is NULL, all the files will be returned and all the
// subdirectories ignored.
// If extension is "/", all the subdirectories will be returned and all the
// files ignored.
// The returned list can optionally be sorted.
const char			**FS_ListFiles (const char *path, const char *extension, bool sort, int *numFiles);

// Frees the memory allocated by FS_ListFiles and FS_ListFilteredFiles
void				FS_FreeFileList (const char **fileList);

// Initializes the file system
void				FS_Init ();

// Shuts down the file system
void				FS_Shutdown ();


#else
FILE_WARNING
#endif   // __FILESYSTEM_H__